package com.yang.modules.common.deploy;

import com.google.common.collect.Maps;
import com.yang.common.amqp.MessageSender;
import com.yang.common.amqp.rabbit.constant.FanoutConstants;
import com.yang.common.core.util.MdcUtil;
import com.yang.common.core.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 服务启动成功回调（用于服务启动后马上通知其他服务刷新服务列表，即时获得请求流量）
 *
 * @author Billyang
 * @date 2024/9/18 14:35
 */
@Slf4j
@Component
public class StartupApplicationRunner implements ApplicationRunner {

    @Autowired
    private MessageSender messageSender;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        MdcUtil.put();
        String application = SpringContextUtil.getProperty("spring.application.name");
        log.info("{} 服务启动成功", application);

        HashMap<String, Object> params = Maps.newHashMap();
        params.put("application", application);
        params.put("type", "startup");
        messageSender.sendFanoutMessage(params, FanoutConstants.DEPLOY.EXCHANGE);
        MdcUtil.remove();
    }
}
